import { defineStore } from 'pinia'

// 用户信息接口
interface UserInfo {
  id: string
  username: string
  email: string
  role: string
  avatar?: string
  phone?: string
  department?: string
  bio?: string
}

// 主题状态管理
export const useThemeStore = defineStore('theme', {
  state: () => ({
    isDark: false
  }),
  actions: {
    toggleTheme() {
      this.isDark = !this.isDark
      this.saveToStorage()
      this.updateHtmlClass()
    },
    setTheme(isDark: boolean) {
      this.isDark = isDark
      this.saveToStorage()
      this.updateHtmlClass()
    },
    updateHtmlClass() {
      if (typeof document !== 'undefined') {
        if (this.isDark) {
          document.documentElement.classList.add('dark')
        } else {
          document.documentElement.classList.remove('dark')
        }
      }
    },
    saveToStorage() {
      if (typeof localStorage !== 'undefined') {
        localStorage.setItem('theme-isDark', JSON.stringify(this.isDark))
      }
    },
    initTheme() {
      // 从localStorage读取主题设置
      if (typeof localStorage !== 'undefined') {
        const savedTheme = localStorage.getItem('theme-isDark')
        if (savedTheme) {
          try {
            this.isDark = JSON.parse(savedTheme)
          } catch (error) {
            this.isDark = false
          }
        }
      }
      this.updateHtmlClass()
    }
  },
  // 持久化存储在actions中手动处理
})

// 用户认证状态管理
export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: '',
    userInfo: {
      id: '',
      username: '',
      email: '',
      role: '',
      avatar: '',
      phone: '',
      department: '',
      bio: ''
    } as UserInfo
  }),
  getters: {
    isAuthenticated: (state) => !!state.token
  },
  actions: {
    // 初始化认证状态
    initAuth() {
      if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
        const token = localStorage.getItem('token')
        if (token) {
          this.token = token
          // 这里可以添加用户信息的恢复逻辑
          const userInfo = localStorage.getItem('userInfo')
          if (userInfo) {
            try {
              this.userInfo = { ...this.userInfo, ...JSON.parse(userInfo) }
            } catch (error) {
              console.warn('Failed to parse user info from localStorage')
            }
          }
        }
      }
    },

    // 登录状态管理
    login(loginData: any) {
      this.token = loginData.token
      this.userInfo = { ...this.userInfo, ...loginData.userInfo }
      if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
        localStorage.setItem('token', loginData.token)
        localStorage.setItem('userInfo', JSON.stringify(loginData.userInfo))
      }
    },
    logout() {
      this.token = ''
      this.userInfo = {
        id: '',
        username: '',
        email: '',
        role: '',
        avatar: '',
        phone: '',
        department: '',
        bio: ''
      }
      if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        // 清除欢迎弹窗标记，下次登录时重新显示
        localStorage.removeItem('hasShownWelcome')
      }
    }
  }
})

// 应用状态管理
export const useAppStore = defineStore('app', {
  state: () => ({
    collapsed: false,
    loading: false
  }),
  actions: {
    toggleCollapsed() {
      this.collapsed = !this.collapsed
    },
    setLoading(loading: boolean) {
      this.loading = loading
    }
  }
}) 